<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Semaphores</title>
<title>CMSIS-RTOS2: Semaphores</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="cmsis.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="printComponentTabs.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 46px;">
  <td id="projectlogo"><img alt="Logo" src="CMSIS_Logo_Final.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">CMSIS-RTOS2
   &#160;<span id="projectnumber">Version 2.1.3</span>
   </div>
   <div id="projectbrief">Real-Time Operating System: API and RTX Reference Implementation</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<div id="CMSISnav" class="tabs1">
    <ul class="tablist">
      <script type="text/javascript">
		<!--
		writeComponentTabs.call(this);
		//-->
      </script>
	  </ul>
</div>
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Usage&#160;and&#160;Description</span></a></li>
      <li><a href="modules.html"><span>Reference</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__CMSIS__RTOS__SemaphoreMgmt.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#typedef-members">Typedefs</a> &#124;
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">Semaphores<div class="ingroups"><a class="el" href="group__CMSIS__RTOS.html">CMSIS-RTOS API v2</a></div></div>  </div>
</div><!--header-->
<div class="contents">

<p>Access shared resources simultaneously from different threads.  
<a href="#details">More...</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:structosSemaphoreAttr__t"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">osSemaphoreAttr_t</a></td></tr>
<tr class="memdesc:structosSemaphoreAttr__t"><td class="mdescLeft">&#160;</td><td class="mdescRight">Attributes structure for semaphore.  <a href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">More...</a><br/></td></tr>
<tr class="separator:structosSemaphoreAttr__t"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="typedef-members"></a>
Typedefs</h2></td></tr>
<tr class="memitem:ga6e1c1c4b04175bb92b013c8f37249f40"><td class="memItemLeft" align="right" valign="top">typedef void *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a></td></tr>
<tr class="separator:ga6e1c1c4b04175bb92b013c8f37249f40"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:ga2a39806ace781a0008a4374ca701b14a"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> (uint32_t max_count, uint32_t initial_count, const <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">osSemaphoreAttr_t</a> *attr)</td></tr>
<tr class="memdesc:ga2a39806ace781a0008a4374ca701b14a"><td class="mdescLeft">&#160;</td><td class="mdescRight">Create and Initialize a Semaphore object.  <a href="#ga2a39806ace781a0008a4374ca701b14a">More...</a><br/></td></tr>
<tr class="separator:ga2a39806ace781a0008a4374ca701b14a"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga9586952051f00285f1482dbe6695bbc4"><td class="memItemLeft" align="right" valign="top">const char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga9586952051f00285f1482dbe6695bbc4">osSemaphoreGetName</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
<tr class="memdesc:ga9586952051f00285f1482dbe6695bbc4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Get name of a Semaphore object.  <a href="#ga9586952051f00285f1482dbe6695bbc4">More...</a><br/></td></tr>
<tr class="separator:ga9586952051f00285f1482dbe6695bbc4"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga7e94c8b242a0c81f2cc79ec22895c87b"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id, uint32_t timeout)</td></tr>
<tr class="memdesc:ga7e94c8b242a0c81f2cc79ec22895c87b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Acquire a Semaphore token or timeout if no tokens are available.  <a href="#ga7e94c8b242a0c81f2cc79ec22895c87b">More...</a><br/></td></tr>
<tr class="separator:ga7e94c8b242a0c81f2cc79ec22895c87b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga0abcee1b5449d7a6928fb9248c690bb6"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
<tr class="memdesc:ga0abcee1b5449d7a6928fb9248c690bb6"><td class="mdescLeft">&#160;</td><td class="mdescRight">Release a Semaphore token up to the initial maximum count.  <a href="#ga0abcee1b5449d7a6928fb9248c690bb6">More...</a><br/></td></tr>
<tr class="separator:ga0abcee1b5449d7a6928fb9248c690bb6"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga7559d4dff3cda9992fc5ab5de3e74c70"><td class="memItemLeft" align="right" valign="top">uint32_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7559d4dff3cda9992fc5ab5de3e74c70">osSemaphoreGetCount</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
<tr class="memdesc:ga7559d4dff3cda9992fc5ab5de3e74c70"><td class="mdescLeft">&#160;</td><td class="mdescRight">Get current Semaphore token count.  <a href="#ga7559d4dff3cda9992fc5ab5de3e74c70">More...</a><br/></td></tr>
<tr class="separator:ga7559d4dff3cda9992fc5ab5de3e74c70"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga81258ce9c67fa89f07cc49d2e136cd88"><td class="memItemLeft" align="right" valign="top"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga81258ce9c67fa89f07cc49d2e136cd88">osSemaphoreDelete</a> (<a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> semaphore_id)</td></tr>
<tr class="memdesc:ga81258ce9c67fa89f07cc49d2e136cd88"><td class="mdescLeft">&#160;</td><td class="mdescRight">Delete a Semaphore object.  <a href="#ga81258ce9c67fa89f07cc49d2e136cd88">More...</a><br/></td></tr>
<tr class="separator:ga81258ce9c67fa89f07cc49d2e136cd88"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Description</h2>
<p>Semaphores are used to manage and protect access to shared resources. Semaphores are very similar to <a class="el" href="group__CMSIS__RTOS__MutexMgmt.html">Mutexes</a>. Whereas a Mutex permits just one thread to access a shared resource at a time, a semaphore can be used to permit a fixed number of threads/ISRs to access a pool of shared resources. Using semaphores, access to a group of identical peripherals can be managed (for example multiple DMA channels).</p>
<div class="image">
<img src="Semaphore.png" alt="Semaphore.png"/>
<div class="caption">
CMSIS-RTOS Semaphore</div></div>
<p> A semaphore object should be initialized to the maximum number of available tokens. This number of available resources is specified as parameter of the <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> function. Each time a semaphore token is obtained with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a> (in <em>available</em> state), the semaphore count is decremented. When the semaphore count is 0 (i.e. <em>depleted</em> state), no more semaphore tokens can be obtained. The thread/ISR that tries to obtain the semaphore token needs to wait until the next token is free. Semaphores are released with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a> incrementing the semaphore count.</p>
<div class="image">
<img src="semaphore_states.png" alt="semaphore_states.png"/>
<div class="caption">
CMSIS-RTOS Semaphore States</div></div>
 <dl class="section note"><dt>Note</dt><dd>The functions <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>, <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7559d4dff3cda9992fc5ab5de3e74c70">osSemaphoreGetCount</a>, and <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a> can be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd>
<dd>
Refer to <a class="el" href="config_rtx5.html#semaphoreConfig">Semaphore Configuration</a> for RTX5 configuration options.</dd></dl>
<h2>Semaphore Use Cases </h2>
<p>Due to their flexibility, semaphores cover a wide range of synchronizing applications. At the same time, they are perhaps the most challenging RTOS object to understand. The following explains a use case for semaphores, taken from the book <a href="http://www.greenteapress.com/semaphores/" target="_blank">The Little Book Of Semaphores</a> by Allen B. Downey which is available for free download.</p>
<p><b>Non-binary Semaphore (Multiplex)</b></p>
<p>A multiplex limits the number of threads that can access a critical section of code. For example, this could be a function accessing DMA resources which can only support a limited number of calls.</p>
<p>To allow multiple threads to run the function, initialize a semaphore to the maximum number of threads that can be allowed. The number of tokens in the semaphore represents the number of additional threads that may enter. If this number is zero, then the next thread trying to access the function will have to wait until one of the other threads exits and releases its token. When all threads have exited the token number is back to n. The following example shows the code for one of the threads that might access the resource:</p>
<div class="fragment"><div class="line"><a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> multiplex_id;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> thread_n (<span class="keywordtype">void</span>) {</div>
<div class="line"> </div>
<div class="line">  multiplex_id = <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(3U, 3U, NULL);</div>
<div class="line">  <span class="keywordflow">while</span>(1) {</div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(multiplex_id, <a class="code" href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a>);</div>
<div class="line">    <span class="comment">// do something</span></div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(multiplex_id);</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --><p><b>Producer/Consumer Semaphore</b></p>
<p>The producer-consumer problem can be solved using two semaphores.</p>
<p>A first semaphore (<span class="XML-Token">empty_id</span>) counts down the available (empty) buffers, i.e. the producer thread can wait for available buffer slots by acquiring from this one.</p>
<p>A second semaphore (<span class="XML-Token">filled_id</span>) counts up the used (filled) buffers, i.e. the consumer thread can wait for available data by acquiring from this one.</p>
<p>It is crucial for the correct behaviour that the threads acquire and release on both semaphores in the given sequence. According to this example one can have multiple producer and/or consumer threads running concurrently.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#define BUFFER_SIZE 10U</span></div>
<div class="line"><span class="preprocessor"></span> </div>
<div class="line"><a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> empty_id = <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(BUFFER_SIZE, BUFFER_SIZE, NULL);</div>
<div class="line"><a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> filled_id = <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(BUFFER_SIZE, 0U, NULL);</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> producer_thread (<span class="keywordtype">void</span>) {</div>
<div class="line">  <span class="keywordflow">while</span>(1) {</div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(empty_id, <a class="code" href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a>);</div>
<div class="line">    <span class="comment">// produce data</span></div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(filled_id);</div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> consumer_thread (<span class="keywordtype">void</span>) {</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">while</span>(1){</div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(filled_id, <a class="code" href="group__CMSIS__RTOS__Definitions.html#ga9eb9a7a797a42e4b55eb171ecc609ddb">osWaitForever</a>);</div>
<div class="line">    <span class="comment">// consume data</span></div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(empty_id);</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --> <hr/><h2 class="groupheader">Data Structure Documentation</h2>
<a name="structosSemaphoreAttr__t" id="structosSemaphoreAttr__t"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">struct osSemaphoreAttr_t</td>
        </tr>
      </table>
</div><div class="memdoc">
<div class="textblock"><p>Specifies the following attributes for the <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> function. </p>
</div><table class="fieldtable">
<tr><th colspan="3">Data Fields</th></tr>
<tr><td class="fieldtype">
<a class="anchor" id="ab74e6bf80237ddc4109968cedc58c151"></a>const char *</td>
<td class="fieldname">
name</td>
<td class="fielddoc">
name of the semaphore <p>Pointer to a constant string with a human readable name (displayed during debugging) of the semaphore object.</p>
<p>Default: <span class="XML-Token">NULL</span> no name specified. </p>
</td></tr>
<tr><td class="fieldtype">
<a class="anchor" id="a6e93b49cb79c12f768d72580c7731f30"></a>uint32_t</td>
<td class="fieldname">
attr_bits</td>
<td class="fielddoc">
attribute bits <p>Reserved for future use (must be set to '0' for future compatibility). </p>
</td></tr>
<tr><td class="fieldtype">
<a class="anchor" id="a1e100dc33d403841ed3c344e3397868e"></a>void *</td>
<td class="fieldname">
cb_mem</td>
<td class="fielddoc">
memory for control block <p>Pointer to a memory for the semaphore control block object. Refer to <a class="el" href="theory_of_operation.html#StaticObjectMemory">Static Object Memory</a> for more information.</p>
<p>Default: <span class="XML-Token">NULL</span> to use <a class="el" href="group__CMSIS__RTOS__MemoryMgmt.html#CMSIS_RTOS_MemoryMgmt_Automatic">Automatic Dynamic Allocation</a> for the semaphore control block. </p>
</td></tr>
<tr><td class="fieldtype">
<a class="anchor" id="aa55a4335d12dc2785dc00fdc292d1159"></a>uint32_t</td>
<td class="fieldname">
cb_size</td>
<td class="fielddoc">
size of provided memory for control block <p>The size (in bytes) of memory block passed with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#a1e100dc33d403841ed3c344e3397868e">cb_mem</a>. For RTX, the minimum value is defined with <a class="el" href="group__rtx5__specific__defines.html#ga0949b67063d0bd427fbee9c816b5d45a">osRtxSemaphoreCbSize</a> (higher values are permitted).</p>
<p>Default: <span class="XML-Token">0</span> as the default is no memory provided with <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#a1e100dc33d403841ed3c344e3397868e">cb_mem</a>. </p>
</td></tr>
</table>

</div>
</div>
<h2 class="groupheader">Typedef Documentation</h2>
<a class="anchor" id="ga6e1c1c4b04175bb92b013c8f37249f40"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a></td>
        </tr>
      </table>
</div><div class="memdoc">
<p>Semaphore ID identifies the semaphore.</p>
<p>Returned by:</p>
<ul>
<li><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a> </li>
</ul>

</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a class="anchor" id="ga2a39806ace781a0008a4374ca701b14a"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> osSemaphoreNew </td>
          <td>(</td>
          <td class="paramtype">uint32_t&#160;</td>
          <td class="paramname"><em>max_count</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">uint32_t&#160;</td>
          <td class="paramname"><em>initial_count</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">const <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#structosSemaphoreAttr__t">osSemaphoreAttr_t</a> *&#160;</td>
          <td class="paramname"><em>attr</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">max_count</td><td>maximum number of available tokens. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">initial_count</td><td>initial number of available tokens. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">attr</td><td>semaphore attributes; NULL: default values. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>semaphore ID for reference by other functions or NULL in case of error.</dd></dl>
<p>The function <b>osSemaphoreNew</b> creates and initializes a semaphore object that is used to manage access to shared resources and returns the pointer to the semaphore object identifier or <span class="XML-Token">NULL</span> in case of an error. It can be safely called before the RTOS is started (call to <a class="el" href="group__CMSIS__RTOS__KernelCtrl.html#ga9ae2cc00f0d89d7b6a307bba942b5221">osKernelStart</a>), but not before it is initialized (call to <a class="el" href="group__CMSIS__RTOS__KernelCtrl.html#gae818f6611d25ba3140bede410a52d659">osKernelInitialize</a>).</p>
<p>The parameter <em>max_count</em> specifies the maximum number of available tokens. A <em>max_count</em> value of 1 creates a binary semaphore.</p>
<p>The parameter <em>initial_count</em> sets the initial number of available tokens.</p>
<p>The parameter <em>attr</em> specifies additional semaphore attributes. Default attributes will be used if set to <span class="XML-Token">NULL</span>.</p>
<dl class="section note"><dt>Note</dt><dd>This function <b>cannot</b> be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
<p><b>Code Example</b> </p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os2_8h.html">cmsis_os2.h</a>&quot;</span>                          <span class="comment">// CMSIS RTOS header file</span></div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a> sid_Semaphore;                  <span class="comment">// semaphore id</span></div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gaa6c32fe2a3e0a2e01f212d55b02e51c7">osThreadId_t</a> tid_Thread_Semaphore;              <span class="comment">// thread id</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> Thread_Semaphore (<span class="keywordtype">void</span> *argument);         <span class="comment">// thread function</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> Init_Semaphore (<span class="keywordtype">void</span>) {</div>
<div class="line"> </div>
<div class="line">  sid_Semaphore = <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>(2U, 2U, NULL);</div>
<div class="line">  <span class="keywordflow">if</span> (sid_Semaphore == NULL) {</div>
<div class="line">    ; <span class="comment">// Semaphore object not created, handle failure</span></div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  tid_Thread_Semaphore = <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#ga48d68b8666d99d28fa646ee1d2182b8f">osThreadNew</a>(Thread_Semaphore, NULL, NULL);</div>
<div class="line">  <span class="keywordflow">if</span> (tid_Thread_Semaphore == NULL) {</div>
<div class="line">    <span class="keywordflow">return</span>(-1);</div>
<div class="line">  }</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">return</span>(0);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> Thread_Semaphore (<span class="keywordtype">void</span> *argument) {</div>
<div class="line">  osStatus_t val;</div>
<div class="line"> </div>
<div class="line">  <span class="keywordflow">while</span> (1) {</div>
<div class="line">    ; <span class="comment">// Insert thread code here...</span></div>
<div class="line"> </div>
<div class="line">    val = <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga7e94c8b242a0c81f2cc79ec22895c87b">osSemaphoreAcquire</a>(sid_Semaphore, 10U);       <span class="comment">// wait for max. 10 ticks for semaphore token to get available</span></div>
<div class="line">    <span class="keywordflow">switch</span> (val) {</div>
<div class="line">      <span class="keywordflow">case</span> <a class="code" href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813ea9e1c9e2550bb4de8969a935acffc968f">osOK</a>:</div>
<div class="line">        ; <span class="comment">// Use protected code here...</span></div>
<div class="line">        <a class="code" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga0abcee1b5449d7a6928fb9248c690bb6">osSemaphoreRelease</a>(sid_Semaphore);              <span class="comment">// return a token back to a semaphore</span></div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">      <span class="keywordflow">case</span> <a class="code" href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813ea8fc5801e8b0482bdf22ad63a77f0155d">osErrorResource</a>:</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">      <span class="keywordflow">case</span> <a class="code" href="cmsis__os2_8h.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813eac24adca6a5d072c9f01c32178ba0d109">osErrorParameter</a>:</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">      <span class="keywordflow">default</span>:</div>
<div class="line">        <span class="keywordflow">break</span>;</div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">    <a class="code" href="group__CMSIS__RTOS__ThreadMgmt.html#gad01c7ec26535b1de6b018bb9466720e2">osThreadYield</a>();                                    <span class="comment">// suspend thread</span></div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --> 
</div>
</div>
<a class="anchor" id="ga9586952051f00285f1482dbe6695bbc4"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">const char * osSemaphoreGetName </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a>&#160;</td>
          <td class="paramname"><em>semaphore_id</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>name as null-terminated string.</dd></dl>
<p>The function <b>osSemaphoreGetName</b> returns the pointer to the name string of the semaphore identified by parameter <em>semaphore_id</em> or <span class="XML-Token">NULL</span> in case of an error.</p>
<dl class="section note"><dt>Note</dt><dd>This function <b>cannot</b> be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>. </dd></dl>

</div>
</div>
<a class="anchor" id="ga7e94c8b242a0c81f2cc79ec22895c87b"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> osSemaphoreAcquire </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a>&#160;</td>
          <td class="paramname"><em>semaphore_id</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">uint32_t&#160;</td>
          <td class="paramname"><em>timeout</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
    <tr><td class="paramdir">[in]</td><td class="paramname">timeout</td><td><a class="el" href="theory_of_operation.html#CMSIS_RTOS_TimeOutValue">Timeout Value</a> or 0 in case of no time-out. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function.</dd></dl>
<p>The blocking function <b>osSemaphoreAcquire</b> waits until a token of the semaphore object specified by parameter <em>semaphore_id</em> becomes available. If a token is available, the function instantly returns and decrements the token count.</p>
<p>The parameter <em>timeout</em> specifies how long the system waits to acquire the token. While the system waits, the thread that is calling this function is put into the <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ThreadStates">BLOCKED</a> state. The parameter <a class="el" href="theory_of_operation.html#CMSIS_RTOS_TimeOutValue">timeout</a> can have the following values:</p>
<ul>
<li>when <em>timeout</em> is <span class="XML-Token">0</span>, the function returns instantly (i.e. try semantics).</li>
<li>when <em>timeout</em> is set to <b>osWaitForever</b> the function will wait for an infinite time until the semaphore becomes available (i.e. wait semantics).</li>
<li>all other values specify a time in kernel ticks for a timeout (i.e. timed-wait semantics).</li>
</ul>
<p>Possible <a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> return values:</p>
<ul>
<li><em>osOK:</em> the token has been obtained and the token count decremented.</li>
<li><em>osErrorTimeout:</em> the token could not be obtained in the given time.</li>
<li><em>osErrorResource:</em> the token could not be obtained when no <em>timeout</em> was specified.</li>
<li><em>osErrorParameter:</em> the parameter <em>semaphore_id</em> is <span class="XML-Token">NULL</span> or invalid.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>May be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a> if the parameter <em>timeout</em> is set to <span class="XML-Token">0</span>.</dd></dl>
<p><b>Code Example</b></p>
<p>Refer to <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </p>

</div>
</div>
<a class="anchor" id="ga0abcee1b5449d7a6928fb9248c690bb6"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> osSemaphoreRelease </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a>&#160;</td>
          <td class="paramname"><em>semaphore_id</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function.</dd></dl>
<p>The function <b>osSemaphoreRelease</b> releases a token of the semaphore object specified by parameter <em>semaphore_id</em>. Tokens can only be released up to the maximum count specified at creation time, see <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. Other threads that currently wait for a token of this semaphore object will be put into the <a class="el" href="group__CMSIS__RTOS__ThreadMgmt.html#ThreadStates">READY</a> state.</p>
<p>Possible <a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> return values:</p>
<ul>
<li><em>osOK:</em> the token has been released and the count incremented.</li>
<li><em>osErrorResource:</em> the token could not be released (maximum token count has been reached).</li>
<li><em>osErrorParameter:</em> the parameter <em>semaphore_id</em> is <span class="XML-Token">NULL</span> or invalid.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>This function may be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>.</dd></dl>
<p><b>Code Example</b></p>
<p>Refer to <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </p>

</div>
</div>
<a class="anchor" id="ga7559d4dff3cda9992fc5ab5de3e74c70"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">uint32_t osSemaphoreGetCount </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a>&#160;</td>
          <td class="paramname"><em>semaphore_id</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>number of tokens available.</dd></dl>
<p>The function <b>osSemaphoreGetCount</b> returns the number of available tokens of the semaphore object specified by parameter <em>semaphore_id</em>. In case of an error it returns <span class="XML-Token">0</span>.</p>
<dl class="section note"><dt>Note</dt><dd>This function may be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>. </dd></dl>

</div>
</div>
<a class="anchor" id="ga81258ce9c67fa89f07cc49d2e136cd88"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname"><a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> osSemaphoreDelete </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga6e1c1c4b04175bb92b013c8f37249f40">osSemaphoreId_t</a>&#160;</td>
          <td class="paramname"><em>semaphore_id</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramdir">[in]</td><td class="paramname">semaphore_id</td><td>semaphore ID obtained by <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>. </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>status code that indicates the execution status of the function.</dd></dl>
<p>The function <b>osSemaphoreDelete</b> deletes a semaphore object specified by parameter <em>semaphore_id</em>. It releases internal memory obtained for semaphore handling. After this call, the <em>semaphore_id</em> is no longer valid and cannot be used. The semaphore may be created again using the function <a class="el" href="group__CMSIS__RTOS__SemaphoreMgmt.html#ga2a39806ace781a0008a4374ca701b14a">osSemaphoreNew</a>.</p>
<p>Possible <a class="el" href="group__CMSIS__RTOS__Definitions.html#ga6c0dbe6069e4e7f47bb4cd32ae2b813e">osStatus_t</a> return values:</p>
<ul>
<li><em>osOK:</em> the semaphore object has been deleted.</li>
<li><em>osErrorParameter:</em> the parameter <em>semaphore_id</em> is <span class="XML-Token">NULL</span> or invalid.</li>
<li><em>osErrorResource:</em> the semaphore is in an invalid state.</li>
<li><em>osErrorISR:</em> <b>osSemaphoreDelete</b> cannot be called from interrupt service routines.</li>
</ul>
<dl class="section note"><dt>Note</dt><dd>This function <b>cannot</b> be called from <a class="el" href="theory_of_operation.html#CMSIS_RTOS_ISR_Calls">Interrupt Service Routines</a>. </dd></dl>

</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Wed Jul 10 2019 15:21:04 for CMSIS-RTOS2 Version 2.1.3 by Arm Ltd. All rights reserved.
	<!--
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.6 
	-->
	</li>
  </ul>
</div>
</body>
</html>
